<template>
  <div class="channelContainer">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>频道管理</span>
      </div>
      <el-table
        :cell-style="{textAlign:'center'}"
        :data="channelData"
        :header-cell-style="{textAlign:'center'}"
        style="width: 100%"
      >
        <el-table-column
          label="频道名称"
          prop="name"
        />
        <el-table-column
          label="频道图片"
          prop="image"
        >
          <template slot-scope="scope">
            <el-popover placement="right" title="" trigger="click">
              <img :src="scope.row.image"  style="height: 200px;width: 200px"/>
              <img slot="reference" :src="scope.row.image" :alt="scope.row.image" style="height: 50px;width: 50px">
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          label="频道状态"
          prop="stauts"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.stauts===1" type="success">激活</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          :render-header="renderHeader"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        style="margin: 20px 0;float: right"
        v-show="queryParams.total>0"
        :total="queryParams.total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getData"
      />
    </el-card>
  </div>
</template>

<script>
import { delChannelList, getChannelList} from "@/api/channel";

export default {
  name: 'Channel',
  data() {
    return {
      //分页查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        total:0
      },
      id:"",
      channelData: []
    }
  },
  created() {
    this.getData()
  },
  methods:{
    //获取频道数据列表
    getData(){
      getChannelList().then(res=>{
        this.channelData=res.data.records
        this.channelData.forEach(item => item.image = item.images.split(',')[0])
        this.queryParams.total=res.data.total
      })
    },
    //编辑频道数据
    edit(row) {
      //打开对话框
      this.$router.push({
        name: "editChannel",
        query: {
          id: row.id
        }
      })
    },
    //删除数据
    del(row){
      const id=row.id
      this.$confirm('您确定删除这条数据么？','提示',{
        confirmButtonText:"确定",
        cancelButtonText:"取消",
        type:"warning"
      }).then(()=>{
        return delChannelList(id).then(()=>{
          this.getData()
        })
      })
    },
    renderHeader(h) {
      return(
        <div>操作
          <el-button type="success" size='mini'  onClick={()=>this.handleAddItem()} style="margin-left:20px">添加</el-button>
        </div>
      )
    },
    //跳转到添加页面
    handleAddItem(){
      this.$router.push('/addChannel')
    }
  }
}
</script>

<style lang="scss" scoped>
.channelContainer {
  padding: 20px;
}
</style>
